import WeCropper from '../../compents/cropper/cropper.js'
const app = getApp();
const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

Page({
  data: {
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    }
  },
  onLoad (opt) {
    

        const { cropperOpt } = this.data;
        var cut = cropperOpt.cut
        cut.x = (width - parseInt(opt.w)) / 2;
        cut.y = (height - parseInt(opt.h)) / 2;
        cut.width = parseInt(opt.w);
        cut.height = parseInt(opt.h
        )
        

        // 若同一个页面只有一个裁剪容器，在其它Page方法中可通过this.wecropper访问实例
        new WeCropper(cropperOpt)
            .on('ready', (ctx) => {
              
                //console.log(`wecropper is ready for work!`)
            })
            .on('beforeImageLoad', (ctx) => {
              
              
                //console.log(`before picture loaded, i can do something`)
                //console.log(`current canvas context: ${ctx}`)
                wx.showToast({
                    title: '上传中',
                    icon: 'loading',
                    duration: 20000
                })
            })
            .on('imageLoad', (ctx) => {
                //console.log(`picture loaded`)
                //console.log(`current canvas context: ${ctx}`)
                wx.hideToast()
          }).pushOrign(opt.pic)

        // 若同一个页面由多个裁剪容器，需要主动做如下处理

    },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  back(){
    wx.navigateBack({});
  },
  getCropperImage() {
    this.wecropper.getCropperImage((src) => {
      if (src) {
        /*
        wx.showLoading({
          title: '上传中...',
        })
        console.log(src)
        wx.uploadFile({
          url: app.globalData.hosts +'Index/getfile', //仅为示例，非真实的接口地址
          filePath: src,
          name: 'file',
          formData: {
            'appid': app.globalData.appid,
            'member_id': app.globalData.member_id
          },
          success: function (res) {
            var d=JSON.parse(res.data);
            console.log(d.data.url)
            wx.hideLoading();
            wx.setStorageSync("coverpic", d.data.url);
            wx.navigateBack();

          },
          fail: function (res){
            wx.showToast({
              title: '上传失败',
              icon:'none'
            })
          
         
        }
        })*/
        wx.setStorageSync("coverpic", src);
        wx.navigateBack({});
      } else {
        console.log('获取图片地址失败，请稍后重试')
      }
    })
  },
  
})